package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.Result;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class ItemService extends ServiceImpl<ItemMapper, Item> implements IItemService {
    @Autowired
    private ItemMapper itemMapper;
    //分页
    @Override
    public PageDTO findAll(Long page, Long size) {
        Page<Item> page1 = new Page(page,size);
        Page<Item> pageList = itemMapper.selectPage(page1, null);
        List<Item> records = pageList.getRecords();
        PageDTO pageDTO = new PageDTO(pageList.getTotal(),pageList.getRecords());
        return pageDTO;
    }
    //根据id查询
    @Override
    public Item findById(Long id) {
        return itemMapper.selectById(id);
    }
    //新增
    @Override
    public Result add(Item item) {
        int insert = itemMapper.insert(item);
        return Result.success();
    }

    //商品上加下架
    @Override
    public Result putItem(Long id, Integer status) {
        Item item = new Item();
        item.setId(id);
        item.setStatus(status);
        itemMapper.updateById(item);
        return Result.success();
    }
    //修改菜品
    @Override
    public Result updateItem(Item item) {
        itemMapper.updateById(item);
        return Result.success();
    }
    //根据id删除

    @Override
    public Result deleteById(Long id) {
        itemMapper.deleteById(id);
        return Result.success();
    }
    //更新库存信息

    @Override
    public Integer Inventory(Long id,Long num) {
        Item item = findById(id);
        item.setStock((int) (item.getStock()-num));
        int i = itemMapper.updateById(item);
        return i;
    }
}
